import { reqAddHospitalSet, reqGetHospiatlSetById, reqUpdateHospitalSet } from '@/api/hopaital/hospitalSet';
import { reqAddHospitalSetParams } from '@/api/hopaital/hospitalSet/types';
import Loading from '@/components/Loading';
import { Button, Card, Form, Input, Space, message } from 'antd'
import React, { useEffect, useState } from 'react'
import { useNavigate, Link, useParams } from 'react-router-dom';

export default function AddOrUpdateHosptal() {
  // 定义效果初始状态
  let [isLoading, setIsLoading] = useState(false)
  let navigate = useNavigate();

  let { id } = useParams();
  let [form] = Form.useForm();
  let onFinish = async (values: reqAddHospitalSetParams) => {
    if (!id) {
      await reqAddHospitalSet(values);
    }
    else {
      await reqUpdateHospitalSet({
        ...values,
        id
      })
    }
    message.success(`${id ? '更新' : '新增'}成功`);
    //跳转
    navigate('/syt/hospital/hospitalSet')
  };
  // 返回
  let back = () => {
    navigate(-1)
  }
  let onFinishFailed = () => { };

  useEffect(() => {
    let getData = async () => {
      if (!id) return;
      setIsLoading(true)
      let result = await reqGetHospiatlSetById(id as string)
      form.setFieldsValue(result)
      setIsLoading(false)
    }
    getData();
    return () => {
    }
  }, [])

  return (
    <div>
      <Card>
        {isLoading && <Loading />}
        <Form
          form={form}
          name="basic"
          labelCol={{ span: 2 }}
          wrapperCol={{ span: 22 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="医院名称"
            name="hosname"
            rules={[{ required: true, message: '医院名称不能为空' }]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="医院编号"
            name="hoscode"
            rules={[{ required: true, message: '医院编号不能为空' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="api基础路径"
            name="apiUrl"
            rules={[{ required: true, message: 'api基础路径不能为空' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="联系人姓名"
            name="contactsName"
            rules={[{ required: true, message: '联系人姓名不能为空' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="联系人手机"
            name="contactsPhone"
            rules={[
              { required: true, message: '联系人手机不能为空' },
              { pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/ }
            ]}
          >
            <Input />
          </Form.Item>


          <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
            <Space>
              <Button type="primary" htmlType="submit">
                保存
              </Button>
              <Button onClick={back}>
                返回
              </Button>
            </Space>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}
